package com.ruoyi.project.mahjong.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.*;
import com.ruoyi.project.mahjong.domain.dto.ProductExchangeDto;
import com.ruoyi.project.mahjong.domain.uyil.MemberLogUtil;
import com.ruoyi.project.mahjong.service.ITIntegralExchangeService;
import com.ruoyi.project.mahjong.service.ITLogManageService;
import com.ruoyi.project.mahjong.service.ITMemberLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.CommodityMapper;
import com.ruoyi.project.mahjong.service.ICommodityService;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-16
 */
@Service
public class CommodityServiceImpl implements ICommodityService
{
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private TMemberServiceImpl tMemberService;
    @Autowired
    private ITIntegralExchangeService itIntegralExchangeService;
    @Autowired
    private ITMemberLogService tMemberLogService;


    /**
     * 查询商品
     *
     * @param id 商品ID
     * @return 商品
     */
    @Override
    public Commodity selectCommodityById(Long id)
    {
        return commodityMapper.selectCommodityById(id);
    }

    /**
     * 查询商品列表
     *
     * @param commodity 商品
     * @return 商品
     */
    @Override
    public List<Commodity> selectCommodityList(Commodity commodity)
    {
        return commodityMapper.selectCommodityList(commodity);
    }

    /**
     * 新增商品
     *
     * @param commodity 商品
     * @return 结果
     */
    @Override
    public int insertCommodity(Commodity commodity)
    {
        commodity.setCreateTime(DateUtils.getNowDate());
        return commodityMapper.insertCommodity(commodity);
    }

    /**
     * 修改商品
     *
     * @param commodity 商品
     * @return 结果
     */
    @Override
    public int updateCommodity(Commodity commodity)
    {
        commodity.setUpdateTime(DateUtils.getNowDate());
        return commodityMapper.updateCommodity(commodity);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteCommodityByIds(Long[] ids)
    {
        return commodityMapper.deleteCommodityByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品ID
     * @return 结果
     */
    @Override
    public int deleteCommodityById(Long id)
    {
        return commodityMapper.deleteCommodityById(id);
    }

    @Override
    public String productExchange(ProductExchangeDto Dto) {
        Commodity commodity =  commodityMapper.selectCommodityByIdAndShopId(Dto.getProductId() ,Dto.getShopId());
         if (ObjectUtil.isNull(commodity)){
             throw new BaseException("暂无商品信息");
         }
         if (commodity.getIntegral()==null){
             throw new BaseException("该商品未设置兑换所需积分");
         }
         Long integral = Long.valueOf(commodity.getIntegral())*Dto.getNumber();

         TMember tMember = tMemberService.selectTMemberById(Dto.getMemberId());
         Long memberIntegral = Long.valueOf(tMember.getIntegral());
         if (memberIntegral<=integral){
             throw new BaseException("会员积分不足");
         }
//         进行兑换 减积分 加 兑换记录
        Long useIntegral= memberIntegral - integral;
        tMember.setIntegral(useIntegral.toString());
         int i = tMemberService.updateTMember(tMember);
         if (i==0){
             throw new BaseException("更改会员积分失败，请重试");
         }
         TIntegralExchange tIntegralExchange = new TIntegralExchange();
        tIntegralExchange.setUserId(Dto.getMemberId());
        tIntegralExchange.setProduct(Dto.getProductId().toString());
        tIntegralExchange.setExchangeNumber(Dto.getNumber());
        tIntegralExchange.setBak1(Dto.getStaffId().toString());
        tIntegralExchange.setBak2(Dto.getShopId().toString());
        tIntegralExchange.setIntegral(useIntegral.toString());
         Date date = new Date();
        tIntegralExchange.setExchangeTime(date);
        tIntegralExchange.setCreateTime(date);
        int i1 = itIntegralExchangeService.insertTIntegralExchange(tIntegralExchange);
        if (i1==0){
            throw new BaseException("增加兑换记录失败，请重试");
        }

/*//        添加操作日志
         TMemberLog tMemberLog = MemberLogUtil.insertMemberLog(Dto.getShopId(),
                Dto.getStaffId(), Dto.getMemberId(),integral.toString(), null,"商品兑换",null,null);
        tMemberLogService.insertTMemberLog(tMemberLog);*/
        return "兑换成功,使用了"+integral+"积分";
    }

    @Override
    public List<Commodity> lishavJftPage(Commodity commodity) {

        return commodityMapper.lishavJftPage(commodity.getShopId());
    }
}
